<?php
/*------------------------------------------------------------------------
 * KnowledgeController.class.php 
 *
 * 知识库	
 * 	
 * Created on 2015-12-16
 *
 * Author: hwc123 <hwc123@139.com>
 * 
 * Copyright (c) 2015 http://www.qingtajiao.com All rights reserved.
 * ------------------------------------------------------------------------
 */

namespace Home\Controller;
class KnowledgeController extends CommonController{
	public $module = 'knowledge';

	public function _initialize(){
		parent::_initialize();
		$this->add_step('知识库', URL('index'));
	}
	// 列表页面
	public function index(){
		// 所属分类
		$category_id = $this->_get('category_id');
		if($category_id){
			$where['category_id'] = $category_id;
		}
		$this->assign('category_id', $category_id);

		// D('Article')->pagesize = 1;
		// $this->pagesize = 1;
		$where['module'] = $this->module;
		$list = D('Article')->get_list($where, '', $this->_get('p', '', 1));
		//处理AJAX分页
		$this->ajax_page($list, D('Article')->count);


		$this->assign('list', $list);
		$this->assign('banner', D('Banner')->get_list($this->module));
		$this->assign('hot_list', $this->hot_list());// 热门文章
		$this->assign('category_list', D('Category')->get_list($this->module));
		$this->assign('hot_tag_list', D('Tags')->get_hot_tag_list('article_' . $this->module));
		$this->display();
	}
	public function search(){
		// 按关键字搜索
		$keyword = $this->_get('keyword');
		if($keyword){
			$where['title'] = array('like', '%' . $keyword . '%');
		}
		// 按标签搜索
		$tags_id = $this->_get('tags_id');
		if($tags_id){
			$tags_name   = D('Tags')->where(array('tags_id' => $tags_id))->getField('content');
			$keyword     = $keyword ? $keyword : $tags_name;
			$join_id_arr = D('TagsJoin')->where(array('tags_id' => $tags_id))->getField('join_id', true);
			$join_id_str = implode(',', $join_id_arr);
			$where['id'] = array('in', $join_id_str);
		}

		$where['module'] = $this->module;
		$list  = D('Article')->get_list($where, '', $this->_get('p', '', 1));
		$count = D('Article')->count;
		//处理AJAX分页
		$this->ajax_page($list, $count);

		$this->assign('count', $count);
		$this->assign('list', $list);
		$this->assign('keyword', $keyword);
		$this->assign('hot_list', $this->hot_list());// 热门文章
		$this->assign('hot_tag_list', D('Tags')->get_hot_tag_list('article_' . $this->module));
		$this->assign('tags_name', $tags_name);
		$this->display();
	}
	// 详情
	public function detail(){
		$article_id = $this->_get('article_id', '对不起，您的操作有误！');
		$info = D('Article')->info($article_id);
		$this->add_step($info['title']);
		// 相关关键字
		$related_keyword = $info['related_keyword'];
		// 更新查看次数
		D('Article')->update_view_num($article_id, $this->user_id, session_id());

		// var_dump($info);
		// 热门文章
		$this->assign('hot_list', $this->hot_list());
		// 相关文章
		$this->assign('related_article_list', $this->related_article_list($related_keyword, $article_id));
		$this->assign('info', $info);
		$this->assign('hot_tag_list', D('Tags')->get_hot_tag_list('article_' . $this->module)); //热门标签
		$this->display();
	}

	// 热门文章
	private function hot_list(){
		$list = D('Article')->get_list(array('module' => $this->module), 'view_num desc,id desc');
		return $list;
	}
	// 相关文章
	private function related_article_list($keyword = '', $article_id = ''){
		$list = D('Article')->related_article_list($this->module, $keyword, $article_id);
		return $list;
	}
}